//
//  ViewController.m
//  My
//
//  Created by Mac on 15-11-23.
//
//

#import "ViewController.h"
#import "InPutViewController.h"
@interface ViewController ()<InputFinshDelegate>
{
    NSMutableArray *name;
    NSMutableArray *tels;
    NSIndexPath *curIndexPath;
}
@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
//    name = @[
//             @"曹操",
//             @"诸葛亮",
//             @"张飞",
//             @"小乔"
//             ];
//    tels = @[
//             @"419",
//             @"419",
//             @"419",
//             @"419"
//             ];
    self.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
//    [def setObject:name forKey:@"name"];
//    [def setObject:tels forKey:@"tels"];
//    [def synchronize];
    
    name = [[def objectForKey:@"name"]mutableCopy];
    tels = [[def objectForKey:@"tels"]mutableCopy];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return name.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.text = name[indexPath.row];
    cell.detailTextLabel.text = tels[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (void)InputFinish:(NSString *)text
{
    NSMutableArray *arr = tels.mutableCopy;
    [arr replaceObjectAtIndex:curIndexPath.row withObject:text];
    tels = arr;
    //修改完，刷新表格
    //保存修改
    NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
    [def setObject:tels forKey:@"tels"];
    [def synchronize];
    
    
    //刷新数据
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    InPutViewController *con = [[InPutViewController alloc]initWithStyle:UITableViewStyleGrouped];
    curIndexPath = indexPath;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    con.tels = cell.detailTextLabel.text;
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [name removeObjectAtIndex:indexPath.row];
    [tels removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
